var offset = 0;
var serviceURL = "/yaws/json_handlers.yaws";
var methods = [ "requestList", "giveHomework" ];

var jsonrpc = imprt("jsonrpc");
var service = new jsonrpc.ServiceProxy(serviceURL, methods);


function next()
{
	offset = offset + 9;
	requestList();
}

function previous()
{
	offset = offset - 9;
	requestList();
}


function show_form(student)
{
	document.getElementById("student").innerHTML = student;
	document.getElementById("homework_list").style.display = "block";
}

function close_info()
{
	document.getElementById("info").style.display = "none";
}

//Validates the form that contains the homework
function check_form()
{
	var form = document.getElementById("homework");
	var submit = true;
	if (form.question.value == "")
	{
		document.getElementById("question_entered").innerHTML = " *";
		submit = false;
	}
	
	if(submit == true)
	{
		var student = document.getElementById("student").innerHTML;
		var result = eval('(' + service.giveHomework(student, form.question.value, form.difficulty.value) + ')');
		document.getElementById("homework_list").style.display = "none";
		document.getElementById("question").value = "";
		if(result.result == "ok")
		{
			document.getElementById("infomessage").innerHTML = "The homework has been sent to " + student + ". You will receive a notification as soon as he/she does the exercise."
			document.getElementById("info").style.display = "block";
		}
		else if(result.result == "error")
		{
			document.getElementById("infomessage").innerHTML = "An error has occurred. Please, try to send the homework again."
			document.getElementById("info").style.display = "block";
		}
	}
}

//Obtains the list of users that study a language
function requestList() {
	try {
		var users = eval('(' + service.requestList(offset) + ')');
		var i = 0;
		var number = 0;
		document.getElementById("add_users").innerHTML = "";
		for (var user in users)
		{
			if(i == 0)
			{
				number = users[i];
				i = i + 1;
			}
			else if(users[i] != undefined)
			{
				if(i % 2 != 0)
				{
					var tr = document.createElement("tr");
					tr.setAttribute("class", "odd");
					
					var td1 = document.createElement("td");
					td1.setAttribute("class", "first");
					
					var anchor = document.createElement("a");
					anchor.setAttribute("onclick", "javascript:show_form('" + users[i].name + "')");
					anchor.setAttribute("href", "#student");
					anchor.appendChild(document.createTextNode(users[i].name));
					td1.appendChild(anchor);
					
					var td2 = document.createElement("td");
					td2.setAttribute("class", "last");
					td2.appendChild(document.createTextNode(users[i].level));
					
					tr.appendChild(td1);
					tr.appendChild(td2);

					document.getElementById("add_users").appendChild(tr);
				}
				else
				{
					var tr = document.createElement("tr");
					
					var td1 = document.createElement("td");
					td1.setAttribute("class", "first");
					
					var anchor = document.createElement("a");
					anchor.setAttribute("onclick", "javascript:show_form('" + users[i].name + "')");
					anchor.setAttribute("href", "#student");
					anchor.appendChild(document.createTextNode(users[i].name));
					td1.appendChild(anchor);
					
					var td2 = document.createElement("td");
					td2.setAttribute("class", "last");
					td2.appendChild(document.createTextNode(users[i].level));
					
					tr.appendChild(td1);
					tr.appendChild(td2);

					document.getElementById("add_users").appendChild(tr);
				}
				i = i + 1;
			}
		}
		if(offset > 0 || number > offset + 9)
		{
			var next = "";
			var previous = "";
			if(offset > 0)

			{
				previous = "<a class=\"previous\" href=\"javascript:previous();\"><strong>Previous</strong></a>";
			}
			if(number > offset + 9)
			{
				next = "<a class=\"next\" href=\"javascript:next();\"><strong>Next</strong></a>";
			}
			document.getElementById("add_users").innerHTML += "<tr><td class=\"first\">" + previous + "</td><td class=\"last\">" + next + "</td></tr>";
		}
	} catch(e) {
		alert(e);
	} 
	return false;
}

